{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e9ec99b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.widgets import Slider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afb32eae",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 参数设置\n",
    "U = 1.0       # 来流速度\n",
    "a = 1.0       # 圆柱半径\n",
    "Gamma_min = -4 * np.pi * U * a  # Γ最小值\n",
    "Gamma_max = 4 * np.pi * U * a   # Γ最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "90af67fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建网格\n",
    "x = np.linspace(-3*a, 3*a, 100)\n",
    "y = np.linspace(-3*a, 3*a, 100)\n",
    "X, Y = np.meshgrid(x, y)\n",
    "R = np.sqrt(X**2 + Y**2)\n",
    "Theta = np.arctan2(Y, X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02700f1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建图形和轴\n",
    "fig, ax = plt.subplots(figsize=(8, 8))\n",
    "ax.set_xlim(-3*a, 3*a)\n",
    "ax.set_ylim(-3*a, 3*a)\n",
    "ax.set_aspect('equal')\n",
    "ax.set_xlabel('x')\n",
    "ax.set_ylabel('y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f05b8316",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制圆柱\n",
    "cylinder = plt.Circle((0, 0), a, color='white', zorder=10)\n",
    "ax.add_patch(cylinder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b57057b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始Γ值\n",
    "Gamma = 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbe1b9cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算初始流函数ψ\n",
    "psi = U * Y * (1 - (a**2)/(X**2 + Y**2)) + (Gamma/(4*np.pi)) * np.log(X**2 + Y**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ddf4ff0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制初始流线\n",
    "contour = ax.contour(X, Y, psi, levels=30, colors='blue')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bed10ecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置标题\n",
    "ax.set_title(f'Flow around a cylinder with Γ = {Gamma:.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4cf29e4",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "# 创建滑条轴\n",
    "slider_ax = plt.axes([0.2, 0.05, 0.6, 0.03])\n",
    "gamma_slider = Slider(slider_ax, 'Γ', Gamma_min, Gamma_max, valinit=Gamma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1be32fc6",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "# 更新函数\n",
    "def update(val):\n",
    "    global contour\n",
    "    Gamma = gamma_slider.val\n",
    "    \n",
    "    # 清除之前的流线\n",
    "    ax.clear()\n",
    "    \n",
    "    # 绘制圆柱\n",
    "    ax.add_patch(plt.Circle((0, 0), a, color='white', zorder=10))\n",
    "    \n",
    "    # 设置坐标轴范围和标题\n",
    "    ax.set_xlim(-3*a, 3*a)\n",
    "    ax.set_ylim(-3*a, 3*a)\n",
    "    ax.set_aspect('equal')\n",
    "    ax.set_xlabel('x')\n",
    "    ax.set_ylabel('y')\n",
    "    \n",
    "    # 计算新的流函数ψ\n",
    "    psi = U * Y * (1 - (a**2)/(X**2 + Y**2)) + (Gamma/(4*np.pi)) * np.log(X**2 + Y**2)\n",
    "    \n",
    "    # 绘制新的流线\n",
    "    contour = ax.contour(X, Y, psi, levels=30, colors='blue')\n",
    "    \n",
    "    # 更新标题\n",
    "    ax.set_title(f'Flow around a cylinder with Γ = {Gamma:.2f}')\n",
    "    \n",
    "    # 重新绘制图形\n",
    "    fig.canvas.draw_idle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e74cccad",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绑定滑条事件\n",
    "gamma_slider.on_changed(update)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b32e38cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 显示图形\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
